// pages/complaint/complaint.js
import api from "../../../utils/request"
Page({
  data: {
    activeTab: "all",
    complaints: [
      {
        id: "1",
        caseNumber: "TS202312010001",
        submitTime: "2023-12-01",
        type: 0, // 0：投诉，1：建议
        description: "小区内噪音污染严重，影响正常休息，希望物业能够加强管理。",
        status: 0, // 0:待回复,1:已回复,2:已评价
        statusText: "待回复",
        statusType: "warning",
        houseAddress: "城市花园1栋1单元1层101",
        replyContent: "",
        evaluationContent: "",
        responderName: "",
      },
      {
        id: "2",
        caseNumber: "JY202312010002",
        submitTime: "2023-11-28",
        type: 1,
        description: "建议在小区内增设儿童游乐设施，方便业主带孩子活动。",
        status: 1,
        statusText: "已回复",
        statusType: "success",
        houseAddress: "城市花园1栋2单元2层201",
        replyContent: "感谢您的建议，我们会考虑在合适的位置增设儿童游乐设施。",
        evaluationContent: "",
        responderName: "张管家",
      },
      {
        id: "3",
        caseNumber: "TS202311250003",
        submitTime: "2023-11-25",
        type: 0,
        description: "电梯经常故障，维修不及时，给业主出行带来很大不便。",
        status: 2,
        statusText: "已评价",
        statusType: "default",
        houseAddress: "城市花园2栋1单元3层301",
        replyContent: "我们已安排专业维修人员检查电梯，并制定了定期维护计划。",
        evaluationContent: "处理及时，满意",
        responderName: "李管家",
      },
    ],
    allComplaints: [], // 存储所有数据
  },

  onLoad() {
    this.setData({
      allComplaints: this.data.complaints,
    })
    this.loadComplaints()
  },

  onShow() {
    this.setData({
      allComplaints: this.data.complaints,
    })
    this.loadComplaints()
  },

  // 加载投诉建议数据
  loadComplaints() {
    wx.showLoading({
      title: "加载中...",
    })

    // 模拟API请求
    setTimeout(async() => {
      // 实际项目中这里应该是API请求
      const result = await api.get('/property/complaint/myList')
      this.setData({
        complaints: result.data,
        allComplaints: result.data
      })

      wx.hideLoading()
    }, 500)
  },

  // 标签页切换
  onTabChange(e) {
    const { name } = e.detail
    this.setData({ activeTab: name })
    this.filterComplaints(name)
  },

  // 筛选投诉建议
  filterComplaints(status) {
    const { allComplaints } = this.data
    let filteredComplaints = allComplaints

    if (status !== "all") {
      filteredComplaints = allComplaints.filter((item) => item.status.toString() === status)
    }

    this.setData({
      complaints: filteredComplaints,
    })
  },

  // 点击投诉建议项
  onItemClick(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/property/complaint-detail/complaint-detail?id=${id}`,
    })
  },

  // 提交投诉建议
  onSubmitComplaint() {
    wx.navigateTo({
      url: "/pages/property/complaint-form/complaint-form",
    })
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.loadComplaints()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1500)
  },
})
